package com.kunbo.cn.controller.core.card;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.entity.core.card.IssuanceCard;
import com.kunbo.cn.entity.core.poundlist.PurchasePoundList;
import com.kunbo.cn.service.core.card.IssuanceCardService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.json.FastJsonUtils;
import com.kunbo.cn.util.retu.BuildBean;
import com.kunbo.cn.util.retu.BuildBean1;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.Pages;
import com.kunbo.cn.util.retu.ResultBean;
import com.kunbo.cn.util.retu.ResultBean1;
/**
 * 发卡管理
 */

@RestController
@RequestMapping("/IssuanceCard")
public class IssuanceCardController {
	@Autowired
	private JdbcTemplate jdbc;
	@Autowired
	private IssuanceCardService service;
	@PostMapping("/add")
	public CommonResult add(IssuanceCard ic) {
		service.save(ic);
		return new CommonResult(true,"操作成功",null);
	}
	/**
	 * 发卡编码生成
	 * @return
	 */
	@GetMapping("/createCode")
	public String createCode(){
		 String prex = "IC";
		 String bill_code= prex+SDate.getSystemDateYMDHMSToString();
		 return bill_code;
	}
	@GetMapping("/cardSerial")
	public String getCardSerial(){
		 String prex = "RK";
		 String card_serial= prex+SDate.getSystemDateYMDHMSToString();//getSystemDateToString
		 return card_serial;
	}
	//IC卡注册接口
	@RequestMapping("/iCRegister")
	public CommonResult ICRegister(String jsonStr) {
		System.out.println(jsonStr);
		IssuanceCard t = FastJsonUtils.toBean(jsonStr, IssuanceCard.class);
		if(StringUtils.isBlank(t.getId_card())) {
			return new CommonResult(false,"身份证号为空",null);
		}
		if(StringUtils.isBlank(t.getCar_number())) {
			return new CommonResult(false,"车牌号不可为空",null);
		}
		System.out.println("身份证号为"+t.getId_card());
		List<Map<String,Object>> list = service.findByIdCard(t.getId_card());
		List<Map<String,Object>> list1 = service.findByIdCardNumber(t.getIc_card_number());
		if(list.size()>0) {
			return new CommonResult(false,"该身份证已经注册",null);
		}
		if(list1.size()>0) {
			return new CommonResult(false,"该IC卡已经注册",null);
		} 
		service.save(t);
		return new CommonResult(true,"IC卡注册成功",null);
		
		
		
	}
	//IC补卡
	@GetMapping("/supplementaryCard")
	public CommonResult supplementaryCard(String idNumber,String cardNumber) {
		List<Map<String,Object>> list = service.findByIdCard(idNumber);
		if(list.size()>0) {
			jdbc.update("update business_issuance_card_manage set "
					+ "ic_card_number = '"+cardNumber+"' where id_card='"+idNumber+"'");
			return new CommonResult(true,"IC卡补卡成功",null);
			
		}else {
			return new CommonResult(false,"对不起，您没有对应的卡注册记录",null);
		}
		
	}
	//查卡，根据卡号返回对应的身份信息和车牌信息
	@RequestMapping("/findByICNumber")
	public CommonResult findByICNumber(String icNumber) {
		if(StringUtils.isBlank(icNumber)) {
			return new CommonResult(false,"IC卡为空",null);
		}
		List<Map<String,Object>> list1 = service.findByIdCardNumber(icNumber);
		if(list1.size()>0) {
			return new CommonResult(true,"获取成功",list1.get(0));
		}else {
			return new CommonResult(false,"该卡没有绑定身份证信息",null);
		}

		
	}
	//销卡，根据IC卡号，删除数据库中对应的记录
	@RequestMapping("/logOffICNumber")
	public CommonResult logOffICNumber(String icNumber) {
		if(StringUtils.isBlank(icNumber)) {
			return new CommonResult(false,"IC卡为空",null);
		}
		int r = service.logOffIC(icNumber);
		if(r>0) {
			return new CommonResult(true,"注销成功",null);
		}else {
			return new CommonResult(false,"该卡为空卡，不可注销",null);
		}

		
	}
	
	
		/**
		 * 修改
		 */
		@PutMapping("/update")
		public CommonResult update(IssuanceCard card) {
			service.update(card);
			return new CommonResult(true,"操作成功",null);
		}
		/**
		 * 补卡
		 *只是修改ic卡号
		 */
		@PutMapping("/PatchCard")
		public CommonResult PatchCard(IssuanceCard card) {
			service.PatchCard(card);
			return new CommonResult(true,"操作成功",null);
		}
		/**
		 * 卡的注销
		 * @param id
		 * @return
		 */
		
		@DeleteMapping("/delete")
		public CommonResult delete(Integer id) {
			service.delete(id);
			return new CommonResult(true,"操作成功",null);
		}
		/**
		 * 详情
		 * 
		 */
		@GetMapping("/findById")
		public IssuanceCard findById(Integer id) {
			IssuanceCard ic = service.findById(id);
			return ic;
		}
		
		@GetMapping("/list")
		public ResultBean1<Pages> exelist(
				@RequestParam String ic_card_number,//IC卡卡号
				@RequestParam String car_number,//车牌号
				@RequestParam String id_card,//身份证号
				@RequestParam String state,//状态
				@RequestParam Integer page,
				@RequestParam Integer pageSize
				){
			ResultBean1<Pages> result  = new ResultBean1<Pages>();
			return BuildBean1.buildResulttoPages(result, "success", "error", service.list(ic_card_number,car_number,id_card,state,page,pageSize));
		}
		/**
		 * 
		 * 修改状态
		 */
		@GetMapping("/updateState")
		public CommonResult updateState(String id ,String state) {
			service.updateState(id,state);
			return new CommonResult(true,"操作成功",null);
		}
		
		/**
		 * 获取司机
		 * @return
		 */
		@PostMapping("/carDriver")
		public ResultBean<List<Map<String,Object>>> carDriver(
				){
			ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
			return BuildBean.buildResult(result, "succes", "error",service.carDriver());
		}

		/**
		 * 获取车牌号
		 * @return
		 */
		@PostMapping("/carNumber")
		public ResultBean<List<Map<String,Object>>> carNumber(
				){
			ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
			return BuildBean.buildResult(result, "succes", "error",service.carNumber());
		}
		
		
}
